package com.hc.ssm.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSON;
import com.hc.ssm.pojo.Users;
import com.hc.ssm.services.UsersServices;

@Controller
@RequestMapping("/users")
public class UsersController {
	@Autowired
	private UsersServices usersServices;

	@RequestMapping(value = "/login")
	public String login(Map<String, Object> map,
			@RequestParam String loginAccount, @RequestParam String loginPwd,
			HttpSession session) {
		Map<String, Object> userInfo = new HashMap<String, Object>();
		userInfo.put("account", loginAccount);
		userInfo.put("password", loginPwd);

		Map<String, Object> map1 = usersServices.login(userInfo);

		if (null == map1) {
			map.put("errorMsg", "用户名或密码错误！");
			return "login";
		}
		System.out.println("");
		int id = (Integer) map1.get("id");
		int pagesize = 5;
		int indexpage = 1;
		session.setAttribute("userInfo", map1);
		return "redirect:/blog/findBlog?id=" + id + "&pagesize=" + pagesize
				+ "&indexpage=" + indexpage;
	}

	@RequestMapping(value = "/updateUserInfo")
	public String updateUserInfo(@RequestParam int id,
			@RequestParam String username, @RequestParam String password) {
		Map<String, Object> userInfo = new HashMap<String, Object>();
		userInfo.put("username", username);
		userInfo.put("password", password);
		userInfo.put("id", id);

		System.out.println(JSON.toJSONString(userInfo));
		boolean bool = usersServices.updateUserInfo(userInfo);

		if (bool) {
			return "redirect:/users/exit";
		}

		return "exditAccount";
	}

	@RequestMapping("/exit")
	public String exit(HttpSession session) {
		session.removeAttribute("userInfo");
		return "login";
	}

	@RequestMapping(value = "findAll")
	public String findAll(Map<String, Object> map, @RequestParam int pagesize,
			@RequestParam int indexpage) {
		Map<String, Object> map1 = new HashMap<String, Object>();
		map1.put("indexpage", pagesize * (indexpage - 1));
		map1.put("pagesize", pagesize);
		List<Map<String, Object>> list = usersServices.findAll(map1);
		Map<String, Object> map2 = usersServices.countAll(map1);

		long tableSize = (Long) map2.get("count");
		System.out.println(JSON.toJSONString(list));

		long pageNum = tableSize % pagesize == 0 ? tableSize / pagesize
				: (tableSize / pagesize + 1);
		map.put("userList", list);
		map.put("pageNum", pageNum);
		map.put("tableSize", tableSize);
		map.put("indexpage", indexpage);
		map.put("pagesize", pagesize);

		return "accountAdmin";
	}

	@RequestMapping(value = "delete")
	public String delete(@RequestParam int id, @RequestParam int pagesize,
			@RequestParam int indexpage) {

		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		usersServices.deleteUser(map);

		return "redirect:/users/findAll?pagesize=" + pagesize + "&indexpage="
				+ indexpage;

	}

	@RequestMapping(value = "add")
	public String add(Users users) {
		usersServices.addUser(users);

		return "redirect:/users/findAll?pagesize=5&indexpage=1";

	}

	@RequestMapping(value = "/test")
	public String test() {
		Map<String, Object> map = new HashMap<String, Object>();
		Users u = new Users();
		u.setAge(15);

		map.put("u", u);

		map.put("id", 1);

		usersServices.test(map);

		return "index";
	}

}
